package com.clean.notification.noti;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.widget.RemoteViews;

import androidx.core.content.ContextCompat;

import com.clean.notification.R;
import com.clean.notification.bean.NotifyDateBean;

import kotlin.random.Random;

/**
 * @author Admin
 */
public class RandomNotifyUtils {
    public static int getPendingFlag() {
        return Build.VERSION.SDK_INT >= 23 ? 67108864 : 134217728;
    }

    public RandomNotifyUtils() {
    }

    public static NotifyDateBean notifyItem(Context mContext) {
        String msg = "";
        String title = "";
        String btnTxt = "";
        String typeName = "";
        int targetId = 0;
        int imageResId = 0;
        int notifyId = 0;
        RemoteViews remoteViewsBig = null;
        RemoteViews remoteViewsMini = null;
        PendingIntent pendingIntent = null;
        int random = Random.Default.nextInt(0, 3);
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(mContext.getPackageName(), "com.clean.cleantotal.compores.start.StartActivity"));
        Bundle bundle = new Bundle();
        switch (random) {
            case 0:
                msg = "Remove unwanted junk.";
                title = "";
                btnTxt = "Clean Now";
                typeName = "cleanup";
                imageResId = R.mipmap.n_clean;
                targetId = getPushNotifyId(1);
                notifyId = getPushNotifyId(1);
                remoteViewsBig = new RemoteViews(mContext.getPackageName(), R.layout.noti_clean_big);
                remoteViewsBig.setTextViewText(R.id.view, btnTxt);
                remoteViewsBig.setTextViewText(R.id.content, Html.fromHtml(msg));
                remoteViewsBig.setImageViewResource(R.id.funType, imageResId);
                remoteViewsMini = new RemoteViews(mContext.getPackageName(), R.layout.noti_clean_small);
                remoteViewsMini.setImageViewResource(R.id.funType, imageResId);

                remoteViewsMini.setTextViewText(R.id.content, Html.fromHtml(msg));
                bundle.putString("from", "clean");
                intent.putExtras(bundle);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                pendingIntent = PendingIntent.getActivity(mContext, 100011, intent, getPendingFlag());
                break;
            case 1:
                msg = "Check Device Usage and Status.";
                title = "";
                btnTxt = "View";
                imageResId = R.mipmap.n_process;
                targetId = getPushNotifyId(2);
                notifyId = getPushNotifyId(2);

                typeName = "process";
                remoteViewsBig = new RemoteViews(mContext.getPackageName(), R.layout.noti_process_big);
                remoteViewsBig.setTextViewText(R.id.view, btnTxt);
                remoteViewsBig.setTextViewText(R.id.content, msg);

                remoteViewsBig.setImageViewResource(R.id.funType, imageResId);
                remoteViewsMini = new RemoteViews(mContext.getPackageName(), R.layout.noti_process_small);

                remoteViewsMini.setImageViewResource(R.id.funType, imageResId);
                remoteViewsMini.setTextViewText(R.id.content, msg);

                bundle.putString("from", "process");

                intent.putExtras(bundle);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                pendingIntent = PendingIntent.getActivity(mContext, 100012, intent, getPendingFlag());
                break;
            case 2:
                msg = "Check Phone Battery Health.";
                title = "";
                btnTxt = "View";
                typeName = "battery";
                imageResId = R.mipmap.green_home_horse;
                targetId = getPushNotifyId(3);
                notifyId = getPushNotifyId(3);
                remoteViewsBig = new RemoteViews(mContext.getPackageName(), R.layout.noti_battrry_big);
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction("android.intent.action.BATTERY_CHANGED");

                Intent batteryIntent = ContextCompat.registerReceiver(mContext, (BroadcastReceiver) null, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);
                int level = batteryIntent.getIntExtra("level", 100);
                remoteViewsBig.setTextViewText(R.id.tvPower, String.valueOf(level));
                remoteViewsBig.setTextViewText(R.id.view, btnTxt);

                remoteViewsBig.setTextViewText(R.id.changeDua, String.valueOf(Random.Default.nextInt(20, 50)));
                remoteViewsBig.setImageViewResource(R.id.funType, imageResId);
                remoteViewsMini = new RemoteViews(mContext.getPackageName(), R.layout.noti_battery_small);
                remoteViewsMini.setImageViewResource(R.id.funType, imageResId);
                remoteViewsMini.setTextViewText(R.id.content, msg);

                bundle.putString("from", "battery");

                intent.putExtras(bundle);

                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                pendingIntent = PendingIntent.getActivity(mContext, 100013, intent, getPendingFlag());
                break;
            case 3:
            default:
                msg = "Check Mobile Device Information.";
                title = "";

                btnTxt = "Clean Now";

                typeName = "device";
                imageResId = R.mipmap.green_home_offer;

                targetId = getPushNotifyId(3);
                notifyId = getPushNotifyId(3);

                remoteViewsBig = new RemoteViews(mContext.getPackageName(), R.layout.noti_device_big);
                remoteViewsBig.setTextViewText(R.id.view, btnTxt);
                remoteViewsBig.setTextViewText(R.id.content, msg);
                remoteViewsBig.setImageViewResource(R.id.funType, imageResId);
                remoteViewsMini = new RemoteViews(mContext.getPackageName(), R.layout.noti_device_small);
                remoteViewsMini.setImageViewResource(R.id.funType, imageResId);

                remoteViewsMini.setTextViewText(R.id.content, msg);

                bundle.putString("from", "device");
                intent.putExtras(bundle);

                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                pendingIntent = PendingIntent.getActivity(mContext, 100014, intent, getPendingFlag());

        }


        NotifyDateBean mNotifyDateBean = new NotifyDateBean();
        mNotifyDateBean.setBtnTxt(btnTxt);
        mNotifyDateBean.setTitle(title);
        mNotifyDateBean.setMsg(msg);

        mNotifyDateBean.setImageResId(imageResId);

        mNotifyDateBean.setTargetId(targetId);

        mNotifyDateBean.setNotifyId(notifyId);
        mNotifyDateBean.setTypeName(typeName);
        mNotifyDateBean.setRemoteViewsBig(remoteViewsBig);

        mNotifyDateBean.setRemoteViewsMini(remoteViewsMini);

        mNotifyDateBean.setRemoteViewsMid(remoteViewsBig);

        mNotifyDateBean.setPendingIntent(pendingIntent);

        return mNotifyDateBean;
    }

    public static int getPushNotifyId(int id) {
        if (id == 1) {
            return 960666 + 123482;
        } else {
            if (id == 2) {
                return 960667 + 123482;
            } else {
                if (id == 3) {
                    return 960668 + 123482;
                } else {
                    if (id == 4) {
                        return 960669 + 123482;
                    } else {
                        return 960669 + 123482;
                    }
                }
            }
        }
    }

}
